package com.chenyue.cm.user.service.impl;


import java.io.Serializable;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.chenyue.cm.base.service.BaseServiceImpl;
import com.chenyue.cm.base.dao.LcBaseMapper;
import com.chenyue.cm.match.dao.PublicAreaMapper;
import com.chenyue.cm.match.domain.PublicArea;
import com.chenyue.cm.user.domain.JdAppUser;
import com.chenyue.cm.user.domain.JdMemberAddressExample;
import com.chenyue.cm.utils.DateUtils;
import org.springframework.beans.factory.annotation.Qualifier;
import com.chenyue.cm.user.dao.JdMemberAddressMapper;
import com.chenyue.cm.user.domain.JdMemberAddress;
import com.chenyue.cm.user.service.JdMemberAddressService;
import org.springframework.stereotype.Service;


/**
*
* @author chenyue-cm
*/
@Service(value = "JdMemberAddressService")
public class JdMemberAddressServiceImpl extends BaseServiceImpl<JdMemberAddress>  implements JdMemberAddressService {

private final JdMemberAddressMapper jdMemberAddressMapper;
	@Resource
	private PublicAreaMapper areaMapper;

	public JdMemberAddressServiceImpl(@Qualifier("jdMemberAddressMapper") JdMemberAddressMapper jdMemberAddressMapper) {
		this.jdMemberAddressMapper = jdMemberAddressMapper;
	}

	@Override
	@Resource(name = "jdMemberAddressMapper")
	public void setBaseMapper(LcBaseMapper<JdMemberAddress, Serializable> baseMapper) {
		this.baseMapper = baseMapper;
	}

	@Override
	public List<JdMemberAddress> getMemberAddressList(String userId) {
		JdMemberAddressExample mMemberAddressExample = new JdMemberAddressExample();
		mMemberAddressExample.createCriteria().andMemberIdEqualTo(userId);
		List<JdMemberAddress> memberAddressList = this.selectByExample(mMemberAddressExample);
		return memberAddressList;
	}

	@Override
	public void saveUserAddress(JdAppUser user,Map<String ,String> map) {
		JdMemberAddress jdMemberAddress = new JdMemberAddress();
		jdMemberAddress.setMemberId(user.getId().toString());
		jdMemberAddress.setBuyerName(map.get("buyerName"));
		jdMemberAddress.setBuyerPhone(map.get("buyerPhone"));
		String provinceName = map.get("provinceName");
		jdMemberAddress.setProvinceName(provinceName);
		String cityName = map.get("cityName");
		jdMemberAddress.setCityName(cityName);
		String areaNam = map.get("areaName");
		jdMemberAddress.setAreaName(areaNam);
		jdMemberAddress.setAddressDetail(map.get("addressDetail"));
		jdMemberAddress.setRemark(map.get("remark"));
		jdMemberAddress.setDefaultFlag(map.get("defaultFlag"));
		Integer provinceId = areaMapper.selectOne(Wrappers.<PublicArea>lambdaQuery().eq(PublicArea::getName, provinceName)).getId();
		jdMemberAddress.setProvinceCode(provinceId);
		Integer cityId = areaMapper.selectOne(Wrappers.<PublicArea>lambdaQuery().eq(PublicArea::getName, cityName)).getId();
		jdMemberAddress.setCityCode(cityId);
		Integer areaId = areaMapper.selectOne(Wrappers.<PublicArea>lambdaQuery().eq(PublicArea::getName, areaNam)).getId();
		jdMemberAddress.setAreaCode(areaId);
		jdMemberAddress.setCreateUser(map.get(user.getUserName()));
		jdMemberAddress.setCreateTime(DateUtil.date().toStringDefaultTimeZone());
		this.save(jdMemberAddress);
	}

	@Override
	public void updateUserAddress(JdAppUser user,Map<String ,String> map) {
		String id = map.get("id");
		JdMemberAddress jdMemberAddress = new JdMemberAddress();
		jdMemberAddress.setId(id);
		jdMemberAddress.setMemberId(user.getId().toString());
		jdMemberAddress.setBuyerName(map.get("buyerName"));
		jdMemberAddress.setBuyerPhone(map.get("buyerPhone"));
		String provinceName = map.get("provinceName");
		jdMemberAddress.setProvinceName(provinceName);
		String cityName = map.get("cityName");
		jdMemberAddress.setCityName(cityName);
		String areaNam = map.get("areaName");
		jdMemberAddress.setAreaName(areaNam);
		jdMemberAddress.setAddressDetail(map.get("addressDetail"));
		jdMemberAddress.setRemark(map.get("remark"));
		jdMemberAddress.setDefaultFlag(map.get("defaultFlag"));
		Integer provinceId = areaMapper.selectOne(Wrappers.<PublicArea>lambdaQuery().eq(PublicArea::getName, provinceName)).getId();
		jdMemberAddress.setProvinceCode(provinceId);
		Integer cityId = areaMapper.selectOne(Wrappers.<PublicArea>lambdaQuery().eq(PublicArea::getName, cityName)).getId();
		jdMemberAddress.setCityCode(cityId);
		Integer areaId = areaMapper.selectOne(Wrappers.<PublicArea>lambdaQuery().eq(PublicArea::getName, areaNam)).getId();
		jdMemberAddress.setAreaCode(areaId);
		jdMemberAddress.setUpdateUser(user.getUserName());
		jdMemberAddress.setUpdateTime(DateUtil.date().toStringDefaultTimeZone());
		this.save(jdMemberAddress);
	}

}
